<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();
$mode = $_GET["mode"];
$getPet =  $managerIUDS->queryData("*", "pet", " WHERE pet_id = ".$_GET["pet_id"], "1", "=");
$getMedicalCare =  $managerIUDS->queryData("medical_care_id,medical_care_name,price", "medical_care","", "2", "");
$getMedicine =  $managerIUDS->queryData("medicine_id,medicine_name,price,medicine_sub_type", "medicine","", "2", "");
$today = getdate();

$day = $today["mday"];
$mouth = $today["mon"];
$year = $today["year"];
if(strlen($mouth)<2){
	$mouth = "0".$mouth;
}
if(strlen($day)<2){
	$day = "0".$day;
}
$date = $day."/".$mouth."/".$year;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Maintenance Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					var result = true;
					if(result){
						$("#maintenance_frm").submit();
					}
				});
				$("#medical_care_name").change(function(){
					var price= 0;
					var amount  = $("#amount").val();
					var mySplitMedicine = $("#medicine_name").val().split("|");
					var mySplitMedicalCare = $("#medical_care_name").val().split("|");
					//alert(mySplitMedicine + " " + mySplitMedicine[1]);
					if($("#amount").val() == "" || $("#amount").val() == null || $("#amount").val() == 0){
						amount = 1;
					}
					if(mySplitMedicine == 0){
						price = parseInt(mySplitMedicalCare[1]);
					}else{
						price = parseInt(amount*mySplitMedicine[1])+parseInt(mySplitMedicalCare[1]);
					}
					
					if(mySplitMedicalCare == 0 && mySplitMedicine==0){
						price = 0;
					}

					if(mySplitMedicalCare == 0 && mySplitMedicine!=0){
						price = amount *mySplitMedicine[1];
					}
					
					$("#price").val(price);
				});
				$("#medicine_name").change(function(){
					var price= 0;
					var amount  = $("#amount").val();
					var mySplitMedicine = $("#medicine_name").val().split("|");
					var mySplitMedicalCare = $("#medical_care_name").val().split("|");
					//alert(mySplitMedicalCare + " " + mySplitMedicalCare[1]);
					if($("#amount").val() == "" || $("#amount").val() == null || $("#amount").val() == 0){
						amount = 1;
					}
					if(mySplitMedicalCare == 0){
						price = amount * mySplitMedicine[1];
					}else{
						price = parseInt(amount *mySplitMedicine[1])+parseInt(mySplitMedicalCare[1]);
					}
					
					if(mySplitMedicine == 0){
						price = mySplitMedicalCare[1];
					}
					
					if(mySplitMedicalCare == 0 && mySplitMedicine==0){
						price = 0;
					}
					$("#price").val(price);
				});
				$(function() {
					$( "#combobox" ).combobox();
				});
				$("#medicine_name").change(function(){
					var mySplitMedicine = $("#medicine_name").val().split("|");
					if($("#medicine_name").val() != "0"){
						if(mySplitMedicine[2] == "S"){
							document.getElementById("amount_label").innerHTML = "จำนวนยา/เม็ด : ";
						}else if(mySplitMedicine[2] == "I"){
							document.getElementById("amount_label").innerHTML = "จำนวนยา/เข็ม : ";
						}else{
							document.getElementById("amount_label").innerHTML = "จำนวนยา/ขวด : ";
						}
					}else{
							document.getElementById("amount_label").innerHTML = "จำนวนยา/หน่วย : ";
					}
				});
					$("#amount").blur(function(){
						var result =false;
						var price= 0;
						var mySplitMedicine = $("#medicine_name").val().split("|");
						var mySplitMedicalCare = $("#medical_care_name").val().split("|");
						$.ajax({
    		    					type: "GET",
									 url: "../actions/medicine_action.php",
    		    					data:{mode:"chaeckAmount",amount : $("#amount").val(),medicineId:mySplitMedicine[0]},
									contentType: "application/json; charset=utf-8", 
									dataType: "json",
    		    					async: false,
    		    		            cache: false, 
    		    					success: function(data){
										if(data.result == "L"){
											result = true;
										}
    		    					},
    		    					error: function(xhr,state,exception) { alert("exception = > " + exception); }
    		    				});//end ajax
					if(result){
						if(mySplitMedicalCare == 0){
							price =  $("#amount").val() * mySplitMedicine[1];
						}else{
							price = parseInt($("#amount").val() *mySplitMedicine[1])+parseInt(mySplitMedicalCare[1]);
						}
						if(mySplitMedicine == 0){
							price = 0;
						}
						if($("#amount").val()  == "" || $("#amount").val()  == null && (mySplitMedicine!=0 && mySplitMedicalCare != 0)){
							price = parseInt(mySplitMedicine[1])+parseInt(mySplitMedicalCare[1]);
						}
						$("#price").val(price);
					}else{
						alert("จำนวนยาที่กรอกมากกว่าจำนวนยาเหลืออยู่");
						$("#amount").val("");
						$("#amount").focus();
					}
				});
				$("#add_medical_care").click(function(){
					window.open('popup_medical_care.php?price='+$("#medical_care_price").val()+'&data='+$("#medical_care_data").val(),'popup','width=550,height=250');
				});
				$("#add_medicine").click(function(){
					window.open('popup_medicine.php?price='+$("#medicine_price").val()+'&data='+$("#medicine_data").val(),'popup','width=550,height=250');
				});
			}); 
		</script>
</head>
<body>
<center>
<form name="maintenance_frm" action="../actions/maintenance_action.php" method="post" id="maintenance_frm">
<table border="0" width="75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;รายละเอียดการรักษาสัตว์เลี้ยง</th>
		</tr>
		
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">ชื่อสัตว์ : </td>
			<td><input type="text" name="pet_name" id="pet_name" value="<?=$getPet["pet_name"]?>"  readonly="readonly"></td>
		</tr>
		<tr>
			<td align="right" valign="middle">วิธีการรักษา :</td>
			<td><input type="button" name="add_medical_care" id="add_medical_care" value="  เลือก  "></td>
		</tr>
		<tr>
			<td align="right">รายการรักษา : </td>
			<td>
				<textarea name="medical_care_list" id="medical_care_list" cols="25" rows="3" readonly="readonly"></textarea>
			</td>
		</tr>
		<tr>
		<td align="right">ราคาการรักษา : </td>
			<td><input type="text" name="medical_care_price" id="medical_care_price" onkeypress="return numbersOnly(event, false);" readonly="readonly"></td>
		</tr>
		<tr>
			<td align="right" valign="middle">เลือกยา :</td>
			<td><input type="button" name="add_medicine" id="add_medicine" value="  เลือก  "></td>
		</tr>
		<tr>
			<td align="right">รายการยารักษา : </td>
			<td>
				<textarea name="medicine_list" id="medicine_list" cols="35" rows="3" readonly="readonly"></textarea>
			</td>
		</tr>
		<tr>
		<td align="right">ราคาค่ายา : </td>
			<td><input type="text" name="medicine_price" id="medicine_price" onkeypress="return numbersOnly(event, false);" readonly="readonly"></td>
		</tr>
		<tr>
			<td align="right">รายละเอียดการรักษา: </td>
			<td>
				<textarea name="maintenance_detail" id="maintenance_detail" cols="25" rows="5"></textarea>
			</td>
		</tr>
		<tr>
			<td align="right">วันที่เข้ารักษา : </td>
			<td>
				<input type="text" name="maintenance_date" id="maintenance_date" value="<?=$date?>" readonly="readonly">
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<button name="save_data" id="save_data">บันทึก</button>
			<input type="button" value="ยกเลิก"	onclick="window.location='pet_list.php';"></td>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value="<?=$mode?>">
<input type="hidden" id="medicine_data" name="medicine_data" value="">
<input type="hidden" id="medical_care_data" name="medical_care_data" value="">
<input type="hidden" id="pet_id" name="pet_id" value="<?=$_GET["pet_id"]?>">
</form>
</center>
</body>
</html>